package admin.forms;

import java.net.URL;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import entities.AutoSite;

public class AutoSiteForm {
	public static TreeMap checkValid(HttpServletRequest request, AutoSite autoSite) {
		TreeMap form = new TreeMap();
		TreeMap checkValid = new TreeMap();
		
		String url = (String) request.getParameter("url");
		String regLink = (String) request.getParameter("regLink");
		String regTitle = (String) request.getParameter("regTitle");
		String regContent = (String) request.getParameter("regContent");
		String regRemove = (String) request.getParameter("regRemove");
		String regAuthor = (String) request.getParameter("regAuthor");
		String scheduleSite = (String) request.getParameter("scheduleSite");
		String category = (String) request.getParameter("category");
		String creator = (String) request.getParameter("creator");
		String isTranslate = (String) request.getParameter("isTranslate");
		String language = (String) request.getParameter("language");
		String translate = (String) request.getParameter("translate");
		
		
		if (url == null || url.equals("")) {
			checkValid.put("url", "Site is not empty!");
		} else {
			try {
				URL surl = new URL(url);
			} catch (Exception e) {
				checkValid.put("url", "Site is not url valid!");
			}
			autoSite.setUrl(url);
		}
		
		if (regLink == null || regLink.equals("")) {
			checkValid.put("regLink", "Title link is not empty!");
			autoSite.setRegLink("");
		} else {
			autoSite.setRegLink(regLink);
		}
		
		if (regTitle == null || regTitle.equals("")) {
			checkValid.put("regTitle", "Title is not empty!");
			autoSite.setRegTitle("");
		} else {
			autoSite.setRegTitle(regTitle);
		}
		
		if (regContent == null || regContent.equals("")) {
			checkValid.put("regContent", "Content is not empty!");
			autoSite.setRegContent("");
		} else {
			autoSite.setRegContent(regContent);
		}
			
		if (regRemove != null && !regRemove.equals(""))
			autoSite.setRegRemove(regRemove);
		
		if (regAuthor != null && !regAuthor.equals(""))
			autoSite.setRegAuthor(regAuthor);
		
		if (category != null && !category.equals(""))
			autoSite.setCategory(category);
		
		if (scheduleSite == null || scheduleSite.isEmpty()) {
			checkValid.put("scheduleSite", "Time schedule site is not emtpy");
		} else {
			try {
				autoSite.setScheduleSite(Integer.parseInt(scheduleSite));
			} catch (Exception e) {
				checkValid.put("scheduleSite", "Time schedule site must be a number!");
			}
		}
		
		if (regAuthor == null || regAuthor.isEmpty()) {
			if (creator == null || creator.isEmpty()) {
				checkValid.put("creator", "Author Name is not empty");
				autoSite.setCreator("");
			} else {
				autoSite.setCreator(creator);
			}
		}
		
		if (isTranslate != null && isTranslate.equalsIgnoreCase("true")) {
			autoSite.setTranslate(new Boolean(isTranslate));
			if (language == null || language.isEmpty()) {
				checkValid.put("language", "Language is not empty.");
				autoSite.setLanguage("");
			} else {
				autoSite.setLanguage(language);
			}
			
			if (translate == null || translate.isEmpty()) {
				checkValid.put("translate", "Translate is not empty.");
				autoSite.setTranslate("");
			} else {
				autoSite.setTranslate(translate);
			}
		}
		
		form.put("checkValid", checkValid);
		form.put("autoSite", autoSite);
		return form;
	}
	
}
